<?php
declare (strict_types = 1);
namespace App\Admin\Controller;

use App\Common\Controller\AbstractController;
use App\Common\Logic\CommonLogic;
use App\Server\Model\Slide;
use App\Server\Model\SlideGroup;
use Hyperf\Di\Annotation\Inject;
use App\Admin\Validate\SlideValidate;

class SlideController extends AbstractController
{

    /**
     * @Inject()
     * @var SlideValidate
     */
    protected $SlideValidate;

    public function index()
    {
        $page = $this->request->query('page');
        if ($page) {
            $param = $this->request->all();
            $slideGroup = new SlideGroup();
            $list = $slideGroup->getListPage([], ['*'], null, null,(int)$param['limit']);
            return $this->success($list['data'], '成功', ['count'=>$list['total']]);
        }
        else
        {
            return $this->render->render('/admin/slide/index');
        }
    }

    public function slideIndex()
    {
        $page = $this->request->query('page');
        $param = $this->request->all();
        if ($page) {
            $where = [];
            if(empty($param['slide_group_id']))
            {
                return $this->error('请求错误');
            }
            $where[] = ['slide_group_id','=',$param['slide_group_id']];
            $slideModel = new Slide();
            $list = $slideModel->getListPage($where, ['*'], 'sort desc', null,(int)$param['limit']);
            return $this->success($list['data'], '成功', ['count'=>$list['total']]);
        }
        else
        {
            $data = [];
            $data['slide_group_id'] = $param['slide_group_id'];
            return $this->render->render('/admin/slide/slide_index',$data);
        }
    }


    public function addGroup()
    {
        $page = $this->request->query('page');
        if ($page) {
            $param = $this->request->all();
            unset($param['page']);
            $errorMessage = $this->SlideValidate->addGroup($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }

            $slideGroup = new SlideGroup();
            $addRes = $slideGroup->dataAdd($param);
            if ($addRes){
                return $this->success([],'创建成功');
            }else{
                return $this->error('创建失败');
            }
        }
        else
        {
            return $this->render->render('/admin/slide/add_group');
        }
    }


    public function addSlide()
    {
        $param = $this->request->all();
        $page = $this->request->query('page');
        if ($page) {
            unset($param['page']);
            $errorMessage = $this->SlideValidate->addSlide($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }
            $slideModel = new Slide();
            unset($param['file']);
            $param['created_at'] = $param['updated_at'] = date('Y-m-d H:i:s',time());
            $updateRes = $slideModel->dataAdd($param);
            if ($updateRes){
                return $this->success([],'添加成功');
            }else{
                return $this->error('添加失败');
            }
        }
        else
        {
            $data = [];
            $data['slide_group_id'] = $param['slide_group_id'];
            return $this->render->render('/admin/slide/add_slide',$data);
        }

    }


    public function editGroup()
    {
        $slideGroup = new SlideGroup();
        $id = $this->request->query('id');
        $page = $this->request->query('page');
        $param = $this->request->all();
        if ($page) {
            unset($param['page']);
            $errorMessage = $this->SlideValidate->editGroup($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }
            $slideGroup = new SlideGroup();
            $updateRes = $slideGroup->dataUpdate([['id','=',$param['id']]],$param);
            if ($updateRes){
                return $this->success([],'修改成功');
            }else{
                return $this->error('修改失败');
            }
        } 
        else 
        {
            $data = [];
            $data['data'] = $slideGroup -> getInfo([['id','=',$id]],['*']);
            return $this->render->render('/admin/slide/edit_group', $data);
        }
    }


    public function editSlide()
    {
        $id = $this->request->query('id');
        $slideModel = new Slide();
        $param = $this->request->all();
        $page = $this->request->query('page');
        if ($page) {
            unset($param['page']);
            $errorMessage = $this->SlideValidate->editSlide($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }
            unset($param['file']);
            $updateRes = $slideModel->dataUpdate([['id','=',$param['id']]],$param);
            if ($updateRes){
                return $this->success([],'修改成功');
            }else{
                return $this->error('修改失败');
            }
        }
        else
        {
            $data = [];
            $data['data'] = $slideModel -> getInfo([['id','=',$id]],['*']);
            $data['slide_group_id'] = $param['slide_group_id'];
            return $this->render->render('/admin/slide/edit_slide', $data);
        }
    }


    public function deleteGroup()
    {
        $slide = new Slide();
        $slideGroup = new SlideGroup();
        $id = $this->request->query('id');
        if(!$id) {
            return $this->error('缺少必要条件');
        }
        $count = $slide->dataCount([["slide_group_id" ,'=', $id]]);
        if ($count > 0) {
            return $this->error('该分组下还存在导航，请先删除导航数据');
        }
        $deleteRes = $slideGroup->dataDelete([['id','=',$id]]);
        if (!$deleteRes) {
            return $this->error('删除失败！');
        }
        return $this->success([],'删除成功！');
    }


    public function deleteSlide()
    {
        $slideModel = new Slide();
        $id = $this->request->query('id');
        if(!$id) {
            return $this->error('缺少必要条件');
        }
        $deleteRes = $slideModel->dataDelete([['id','=',$id]]);
        if (!$deleteRes) {
            return $this->error('删除失败！');
        }
        return $this->success([],'删除成功！');
    }
}
